package com.example.papercut.controller;

import com.example.papercut.entity.UserCreation;
import com.example.papercut.service.UserCreationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/creations")
public class UserCreationController {

    private final UserCreationService creationService;

    @Autowired
    public UserCreationController(UserCreationService creationService) {
        this.creationService = creationService;
    }

    /**
     * POST /api/creations
     * 2.4: 保存用户创作结果
     * @param creation 包含用户 ID，模板 ID，纹理 ID，自定义数据等
     */
    @PostMapping
    public ResponseEntity<String> saveCreation(@RequestBody UserCreation creation) {
        try {
            // 实际项目中，user.id 需从认证信息中获取
            creationService.saveCreation(creation);
            return ResponseEntity.ok("创作记录保存成功");
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("保存失败: " + e.getMessage());
        }
    }

    /**
     * GET /api/creations/user/{userId}
     * 3.2: 获取用户创作历史
     */
    @GetMapping("/user/{userId}")
    public ResponseEntity<List<UserCreation>> getUserCreations(@PathVariable Integer userId) {
        List<UserCreation> creations = creationService.getCreationsByUserId(userId);
        return ResponseEntity.ok(creations);
    }
    
    /**
     * GET /api/creations/{id}
     * 3.2: 获取单个创作记录详情
     */
    @GetMapping("/{id}")
    public ResponseEntity<UserCreation> getCreationDetails(@PathVariable Integer id) {
        UserCreation creation = creationService.getCreationDetails(id);
        if (creation == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(creation);
    }
}